{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1.3. Introducing the multidimensional array in NumPy for fast array computations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "n = 1000000\n",
    "x = [random.random() for _ in range(n)]\n",
    "y = [random.random() for _ in range(n)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([0.926, 0.722, 0.962], [0.291, 0.339, 0.819])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[:3], y[:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1.217, 1.061, 1.781]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z = [x[i] + y[i] for i in range(n)]\n",
    "z[:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "101 ms ± 5.12 ms per loop (mean ± std. dev. of 7 runs,\n",
      "    10 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit [x[i] + y[i] for i in range(n)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "xa = np.array(x)\n",
    "ya = np.array(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.926,  0.722,  0.962])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xa[:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1.217,  1.061,  1.781])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "za = xa + ya\n",
    "za[:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.09 ms ± 37.3 µs per loop (mean ± std. dev. of 7 runs,\n",
      "    1000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit xa + ya"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.94 ms ± 4.44 µs per loop (mean ± std. dev. of 7 runs\n",
      "    100 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit sum(x)  # pure Python"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "298 µs ± 4.62 µs per loop (mean ± std. dev. of 7 runs,\n",
      "    1000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit np.sum(xa)  # NumPy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "d = [abs(x[i] - y[j])\n",
    "     for i in range(1000)\n",
    "     for j in range(1000)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0.635, 0.587, 0.106]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d[:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "da = np.abs(xa[:1000, np.newaxis] - ya[:1000])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.635,  0.587,  ...,  0.849,  0.046],\n",
       "       [ 0.431,  0.383,  ...,  0.646,  0.158],\n",
       "       ...,\n",
       "       [ 0.024,  0.024,  ...,  0.238,  0.566],\n",
       "       [ 0.081,  0.033,  ...,  0.295,  0.509]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "da"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "134 ms ± 1.79 ms per loop (mean ± std. dev. of 7 runs,\n",
      "    1000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit [abs(x[i] - y[j]) \\\n",
    "         for i in range(1000) \\\n",
    "         for j in range(1000)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.54 ms ± 48.9 µs per loop (mean ± std. dev. of 7 runs\n",
      "    1000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit np.abs(xa[:1000, np.newaxis] - ya[:1000])"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
